package com.beone.smart.smartBeauty.controller.webApi;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.beone.smart.smartBeauty.exception.GlobalException;
import com.beone.smart.smartBeauty.model.entity.RecommendArticleDO;
import com.beone.smart.smartBeauty.model.form.RecommendCreateForm;
import com.beone.smart.smartBeauty.model.form.RecommendUpdateForm;
import com.beone.smart.smartBeauty.model.result.ResultEntity;
import com.beone.smart.smartBeauty.model.result.ResultTool;
import com.beone.smart.smartBeauty.service.intf.RecommendService;
import com.beone.smart.smartBeauty.utils.LocalTimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * @Date: 2019/9/26
 * @Author: yangjie
 * desc:  用户-模板配置管理
 **/
@Api(description = "推荐方案")
@Slf4j
@RestController
@RequestMapping(value = "/recommend")
public class RecommendController {
    @Autowired
    private RecommendService recommendService;

    @ApiOperation(value = "获取方案列表", notes = "可分页")
    @GetMapping(value = "/list")
    public ResultEntity list(@RequestParam(value = "pageSize", required = false, defaultValue = "10") long pageSize,
                             @RequestParam(value = "pageNum", required = false, defaultValue = "1") long pageNum,
                             @RequestParam(value = "searchKey", required = false) String searchKey) {
        Page<RecommendArticleDO> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<RecommendArticleDO> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(searchKey)) {
            queryWrapper.like(RecommendArticleDO::getTitle, searchKey);
        }
        return ResultTool.ok(recommendService.page(page, queryWrapper));
    }

    @ApiOperation(value = "新建推荐方案", notes = "type为数字")
    @PostMapping(value = "/create")
    public ResultEntity create(@RequestBody RecommendCreateForm recommendCreateForm) {
        RecommendArticleDO recommendArticleDO = new RecommendArticleDO();
        BeanUtils.copyProperties(recommendCreateForm, recommendArticleDO);
        recommendArticleDO.setCreateDate(LocalTimeUtil.getDateTime());
        recommendArticleDO.setReadNumber(0);
        return ResultTool.ok(recommendService.save(recommendArticleDO));
    }

    @ApiOperation(value = "修改推荐方案")
    @PostMapping(value = "/update")
    public ResultEntity update(@RequestBody RecommendUpdateForm recommendUpdateForm) {
        RecommendArticleDO recommendArticleDO = new RecommendArticleDO();
        BeanUtils.copyProperties(recommendUpdateForm, recommendArticleDO);
        return ResultTool.ok(recommendService.updateById(recommendArticleDO));
    }

    @ApiOperation(value = "依据id获取推荐方案详情")
    @ApiImplicitParam(name = "id", value = "方案id", required = true, dataType = "String")
    @GetMapping(value = "/getOne")
    public ResultEntity getOne(@RequestParam @NotBlank String id) {
        RecommendArticleDO recommendArticleDO = recommendService.getById(id);
        if (recommendArticleDO == null) {
            throw new GlobalException(ResultTool.HttpResponseCode.NOT_FOUND, "查询为null，请检查id");
        }
        recommendArticleDO.setReadNumber(recommendArticleDO.getReadNumber() + 1);
        recommendService.updateById(recommendArticleDO);
        return ResultTool.ok(recommendArticleDO);
    }

    @ApiOperation(value = "删除方案")
    @ApiImplicitParam(name = "id", value = "方案id", required = true, dataType = "String")
    @GetMapping(value = "/delete")
    public ResultEntity delete(@RequestParam @NotBlank String id) {
        RecommendArticleDO recommendArticleDO = recommendService.getById(id);
        if (recommendArticleDO == null) {
            throw new GlobalException(ResultTool.HttpResponseCode.NOT_FOUND, "查询为null，请检查id");
        }
        return ResultTool.ok(recommendService.removeById(id));
    }
}
